def solution(n: int, a: list) -> int:
    # PLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    # write code here
    max_value = 0
    for i in range(n - 1):
        for j in range(i + 1, len(a)):
            distance = min(abs(j - i), n - abs(j - i))
            current_value = (a[i] + a[j]) * distance
            if current_value > max_value:
                max_value = current_value
    return max_value


if __name__ == '__main__':
    print(solution(n=3, a=[1, 2, 3]) == 5)
    print(solution(n=4, a=[4, 1, 2, 3]) == 12)
    print(solution(n=5, a=[1, 5, 3, 7, 2]) == 24)
